﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Grizzy_X
{
    public partial class LayerPropForm : Form
    {
        public LayerPropForm()
        {
            InitializeComponent();

            //显示已经添加过的图层属性数据
            if (Controller.DBExists == true)
            {
                listView1.Items.Clear();
                Persistance.cmd.CommandText = "SELECT * FROM 已有图层属性数据";
                Persistance.myDataAdapter.SelectCommand = Persistance.cmd;
                DataSet myDataSet = new DataSet();
                Persistance.myConnection.Open();//打开链接，将已有图层属性数据读入
                int numberOfRows = Persistance.myDataAdapter.Fill(myDataSet, "已有图层属性数据");
                Persistance.myConnection.Close();
                DataTable myDataTable = myDataSet.Tables["已有图层属性数据"];
                if (numberOfRows == 0)
                {
                    listView1.Items.Add("暂无记录");
                }
                else listView1.Items.Clear();
                for (int i = 0; i < myDataTable.Rows.Count; i++)
                {
                    object[] temp = myDataTable.Rows[i].ItemArray.ToArray();
                    string[] attriValues = new string[4];
                    attriValues[0] = temp[0].ToString();
                    attriValues[1] = (string)temp[1];
                    attriValues[2] = (string)temp[2];
                    attriValues[3] = (string)temp[3];
                    ListViewItem value = new ListViewItem(attriValues);
                    listView1.Items.Add(value);
                }
            }
            else
            {
                listView1.Items.Add("暂未建立数据库。");
            }
        }

        public void initializeForm(int curLayerID)
        {
            WaitForChangeLayerIndex = curLayerID;
            //初始化
            LnametextBox.Text = Controller.MyMap.Layer_List[curLayerID - 1].Name;
            switch (Controller.MyMap.Layer_List[curLayerID - 1].Layer_Type)
            {
                case CLayer.LayerType.CPointLayer:
                    typeTextBox.Text = "点";
                    break;
                case CLayer.LayerType.CLineLayer:
                    typeTextBox.Text = "线";
                    break;
                case CLayer.LayerType.CPolygonLayer:
                    typeTextBox.Text = "多边形";
                    break;
                case CLayer.LayerType.CTextLayer:
                    typeTextBox.Text = "文本";
                    break;
            }
            typeTextBox.Enabled = false;
            //初始化字段表
            for (int i = 0; i < Controller.MyMap.Layer_List[curLayerID - 1].AttriFields.Count; i++)
            {
                EditAttri_DataGrid.Rows.Add(Controller.MyMap.Layer_List[curLayerID - 1].AttriFields[i][0], Controller.MyMap.Layer_List[curLayerID - 1].AttriFields[i][1], Controller.MyMap.Layer_List[curLayerID - 1].AttriFields[i][2], Controller.MyMap.Layer_List[curLayerID - 1].AttriFields[i][3]);
            }
        }

        //确定增加字段
        private List<string[]> fieldAdded = new List<string[]>();
        private void addfieldbutton_Click(object sender, EventArgs e)
        {
            NewField newField = new NewField();
            newField.ShowDialog();
            if (newField.needAdd == true)
            {
                EditAttri_DataGrid.Rows.Add(newField.field_Name, newField.field_Type, newField.field_Length, newField.field_Des);
                string[] fields = new string[2];
                switch (newField.field_Type)
                {
                    case "整型(Int)":
                        fields[0] = newField.field_Name; fields[1] = "int";
                        fieldAdded.Add(fields);
                        break;
                    case "字符串(String)":
                        fields[0] = newField.field_Name; fields[1] = "string(" + newField.field_Length + ")";
                        fieldAdded.Add(fields);
                        break;
                    case "布尔型(Bool)":
                        fields[0] = newField.field_Name; fields[1] = "BIT";
                        fieldAdded.Add(fields);
                        break;
                    case "浮点型(Float)":
                        fields[0] = newField.field_Name; fields[1] = "float";
                        fieldAdded.Add(fields);
                        break;
                    case "比特型(Byte)":
                        fields[0] = newField.field_Name; fields[1] = "byte";
                        fieldAdded.Add(fields);
                        break;
                }
            }
            else
            {
            }
        }

        private List<string> fieldDeleted = new List<string>();
        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("删除字段会造成属性数据丢失，确定吗？", "警告") == DialogResult.OK)
            {
                fieldDeleted.Add(EditAttri_DataGrid.CurrentRow.Cells[0].Value.ToString());
                EditAttri_DataGrid.Rows.RemoveAt(EditAttri_DataGrid.CurrentRow.Index);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private int WaitForChangeLayerIndex;

        //确定修改属性
        private void OKbutton_Click(object sender, EventArgs e)
        {
            Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].Name = LnametextBox.Text;
            Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].Description = DescritextBox.Text;

            Persistance.cmd.CommandText = "DELETE FROM 已有图层属性数据 WHERE 图层ID = ";
            Persistance.cmd.CommandText += WaitForChangeLayerIndex.ToString() + ";";
            try
            {
                Persistance.myConnection.Open();
                int result = Persistance.cmd.ExecuteNonQuery();
                Persistance.myConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库错误" + ex.ToString());
            }
            Persistance.cmd.CommandText = "INSERT INTO 已有图层属性数据(图层ID, 图层名称,图层类型,描述)VALUES(";
            Persistance.cmd.CommandText += Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].GetID().ToString() + ",'" + Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].Name + "','"  + typeTextBox.Text + "','" + Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].Description + "');";
            try
            {
                Persistance.myConnection.Open();
                int result = Persistance.cmd.ExecuteNonQuery();
                Persistance.myConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库错误" + ex.ToString());
            }



            List<string[]> fields = new List<string[]>();
            for (int i = 0; i < EditAttri_DataGrid.Rows.Count; i++)
            {
                string[] temp = new string[4];
                temp[0] = EditAttri_DataGrid.Rows[i].Cells[0].Value.ToString();//字段名
                temp[1] = EditAttri_DataGrid.Rows[i].Cells[1].Value.ToString();//字段类型
                temp[2] = EditAttri_DataGrid.Rows[i].Cells[2].Value.ToString();//字段长度
                temp[3] = EditAttri_DataGrid.Rows[i].Cells[3].Value.ToString();//字段描述
                fields.Add(temp);
            }
            //删除不要的字段
            for (int i = 0; i < fieldDeleted.Count; i++)
            {
                Persistance.cmd.CommandText = "ALTER TABLE ";
                Persistance.cmd.CommandText += Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].Name;
                Persistance.cmd.CommandText += " DROP COLUMN ";
                Persistance.cmd.CommandText += fieldDeleted[i] + ";";
                try
                {
                    Persistance.myConnection.Open();
                    int result = Persistance.cmd.ExecuteNonQuery();
                    Persistance.myConnection.Close();
                }
                catch(Exception ex)
                {
                    MessageBox.Show("数据库错误" + ex.ToString());
                }
            }

            //弄新增的字段
            for (int i = 0; i < fieldAdded.Count; i++)
            {
                Persistance.cmd.CommandText = "ALTER TABLE ";
                Persistance.cmd.CommandText += Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].Name;
                Persistance.cmd.CommandText += " ADD COLUMN ";
                Persistance.cmd.CommandText += fieldAdded[i][0] + " " + fieldAdded[i][1] + ";";
                try
                {
                    Persistance.myConnection.Open();
                    int result = Persistance.cmd.ExecuteNonQuery();
                    Persistance.myConnection.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据库错误" + ex.ToString());
                }

            }
            Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].AttriFields = fields;
            this.Close();

            /*Persistance.cmd.CommandText = "DROP TABLE " + Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].Name + ";";
            try
            {
                Persistance.myConnection.Open();
                int result = Persistance.cmd.ExecuteNonQuery();
                Persistance.myConnection.Close();
            }
            catch
            {
                MessageBox.Show("数据库发生错误！请检查输入。");
                Persistance.myConnection.Close();
            }

            //Persistance.cmd.CommandText = "CREATE TABLE ";
            switch (typeTextBox.Text)
            {
                case "点":
                    {
                        Persistance.cmd.CommandText = "CREATE TABLE " +
                            Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].Name +
                            " (" +
                            " 点对象ID int PRIMARY KEY ";
                        break;
                    }
                case "线":
                    {
                        Persistance.cmd.CommandText = "CREATE TABLE "
                            + Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].Name +
                             " (" +
                             " 线对象ID int PRIMARY KEY ";
                        break;
                    }
                case "多边形":
                    {
                        Persistance.cmd.CommandText = "CREATE TABLE " +
                            Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].Name +
                            " (" +
                            " 面对象ID int PRIMARY KEY";
                        break;
                    }
                case "文本":
                    {
                        Persistance.cmd.CommandText = "CREATE TABLE " +
                            Controller.MyMap.Layer_List[WaitForChangeLayerIndex - 1].Name +
                            " (" +
                            " 文本对象ID int PRIMARY KEY";
                        break;
                    }
            }

            //为新增图层创建一张属性表

            if (EditAttri_DataGrid.Rows.Count == 0)
            {
                Persistance.cmd.CommandText += ");";
            }
            else
            {
                Persistance.cmd.CommandText += ",";
                for (int i = 0; i < EditAttri_DataGrid.Rows.Count; i++)
                {
                    Persistance.cmd.CommandText += EditAttri_DataGrid.Rows[i].Cells[0].Value.ToString() + " ";
                    switch (EditAttri_DataGrid.Rows[i].Cells[1].Value.ToString())
                    {
                        case "整型(Int)":
                            Persistance.cmd.CommandText += "int";
                            if (EditAttri_DataGrid.Rows[i].Cells[3].Value.ToString().Contains("不允许为空"))
                                Persistance.cmd.CommandText += " NOT NULL";
                            break;
                        case "字符串(String)":
                            Persistance.cmd.CommandText += "string";
                            string length = EditAttri_DataGrid.Rows[i].Cells[2].Value.ToString();
                            Persistance.cmd.CommandText += "(" + length + ")";
                            if (EditAttri_DataGrid.Rows[i].Cells[3].Value.ToString().Contains("不允许为空"))
                                Persistance.cmd.CommandText += " NOT NULL";
                            break;
                        case "布尔型(Bool)":
                            Persistance.cmd.CommandText += "BIT,";
                            break;
                        case "浮点型(Float)":
                            Persistance.cmd.CommandText += "float";
                            if (EditAttri_DataGrid.Rows[i].Cells[3].Value.ToString().Contains("不允许为空"))
                                Persistance.cmd.CommandText += " NOT NULL";
                            break;
                        case "比特型(Byte)":
                            Persistance.cmd.CommandText += "Byte";
                            if (EditAttri_DataGrid.Rows[i].Cells[3].Value.ToString().Contains("不允许为空"))
                                Persistance.cmd.CommandText += " NOT NULL";
                            break;
                    }
                    if (i == EditAttri_DataGrid.Rows.Count - 1)
                        Persistance.cmd.CommandText += ");";
                    else
                        Persistance.cmd.CommandText += ",";
                }
            }
            try
            {
                Persistance.myConnection.Open();
                int result = Persistance.cmd.ExecuteNonQuery();
                Persistance.myConnection.Close();
                this.Close();
            }
            catch
            {
                MessageBox.Show("数据库发生错误！请检查输入。");
            }*/
        }
    }
}
